<template>
    <div class="block block-bottom">
        <el-row class="block-item nomargin" v-if="isShowEx">
            <el-col style="width:500px;min-width:500px;">
                <dayLineRateChr></dayLineRateChr>
            </el-col>
            <el-col style="width:500px;min-width:500px;">
                <dayLineRateChr :isTop="true"></dayLineRateChr>
            </el-col>
        </el-row>
        <el-row class="block-item">
            <el-button :type="isQry?'warning':'success'" @click="startQuery" size="mini" class="ml10">{{isQry?"暂停":"开始"}}</el-button>
            <el-button type="primary" @click="reLoad" size="mini">重载配置</el-button>
            <el-button type="primary" @click="saveConfig" size="mini">保存配置</el-button>
            <exSwitch v-model="isShowEx" label="是否显示图形" size="mini" class="mt5"></exSwitch>

            <span class="linkarea">
                <search @saveConfig="saveConfig"></search>
                <newStockInfo></newStockInfo>
                <el-button type="text" @click="openLoginPage" class="ml10" size="mini">打开东财</el-button>
                <cfgMgr @saveConfig="saveConfig"></cfgMgr>
                <!-- <talkInfo></talkInfo>
                <payInfo></payInfo> -->
            </span>
            
            <!-- <el-button type="text" @click="openInfoPage" class="ml20" style="float:right">打开链接页面</el-button> -->
            <!-- <el-button type="text" @click="clearAll" class="ml20" style="float:right">清空</el-button> -->
        </el-row>
        <el-row class="block-item">
            <el-col class="colitem block-bottom" style="width:210px;min-width:210px">
                <bigMoneyInfo></bigMoneyInfo>
                <gangtongInfo></gangtongInfo>
            </el-col>
            <el-col class="colitem block-bottom" style="width:240px;min-width:240px;">
                <jjListCom :list="cfg.jjList"></jjListCom>
                <watchList :list="cfg.favList"></watchList>
            </el-col>
            <el-col class="colitem block-bottom" style="width:150px;min-width:150px">
                <div class="box">
                    <div class="title">异常信息</div>
                    <div class="list maxList" ref="exceptionEl">
                        <el-row v-for="(item,idx) in exceptionList" :key="idx" class="mt5">
                            <div v-html="item"></div>
                            <br/>
                        </el-row>
                    </div>
                </div>
            </el-col>
            <el-col class="colitem block-bottom" style="width:220px;min-width:220px" v-for="(witem,widx) in watchList" :key="widx">
                <stockItem :sleepTime="cfg.sleepTime" :info="witem"></stockItem>
            </el-col>
        </el-row>
    </div>
</template>


<script>
import Vue from "vue";
import watchList from "./comp/watchList.vue";
import stockItem from "./comp/stockItem.vue";
import jjListCom from "./comp/jjList.vue";
import gangtongInfo from "./comp/gangtongInfo.vue";
import bigMoneyInfo from "./comp/bigMoneyInfo.vue";
import dayLineRateChr from "./comp/dayLineRateChr.vue";
import cfgMgr from "./comp/cfgMgr.vue";
import search from "~/components/stock/search.vue";
import newStockInfo from "~/components/stock/newStockInfo.vue";


import talkInfo from "~/components/pay/talkInfo.vue";
import payInfo from "~/components/pay/payInfo.vue";

export default {
    props:["cfg"],
    components:{
        stockItem,
        watchList,
        jjListCom,
        gangtongInfo,
        bigMoneyInfo,
        dayLineRateChr,
        cfgMgr,
        search,
        payInfo,
        talkInfo,
        newStockInfo
    },
    data() {
        
        return {
            isQry: false,
            isShowEx: true,
            exceptionList:[],  //异常信息
        }
    },
    computed:{
        watchList(){
            return this.cfg.favList.filter((item)=>{return item.watch})
        }
    },
    provide () {
      return {
        cfg: this.cfg,
        isQry:()=>{
            // var cur = this.$dayjs().format("HH:mm:ss");
            // if(cur>"15:10:00") return false;
            return this.isQry;
        } ,
        getSleepTime:()=>{
            var sleep = this.cfg.sleepTime|| this.$config.defSleepTime;
            if(sleep<1000 || isNaN(sleep)) sleep = this.$config.defSleepTime;
            return sleep;
        },
        isMoneyException: this.isMoneyException,
        addException: this.addException,
      }
    },

    mounted() {
        if(window.electron){
        }
        
        var cur = this.$dayjs().format("HH:mm:ss");
        //if(cur<"09:25:00"){
        //    if(!sessionStorage.getItem("todayFirstOpen")) {
        //        this.openLoginPage();
        //        sessionStorage.setItem("todayFirstOpen",true)
        //    }
        //}
    },

    methods: {
        reLoad(){
            this.$emit("reLoad")
        },
        saveConfig(){
            this.$emit("saveConfig")
        },
        startQuery(){
            this.isQry = !this.isQry;
        },
        isMoneyException(list,key,name){
            if(!list || list.length<2) return;
            var lastitem = list[list.length-1];
            if(lastitem[key] == list[list.length-2][key]){
                 return false; //最后的两笔方向一致
            }else{
                var preItem = null;
                for(var i=list.length-6;i<list.length-1;i++){
                    var item = list[i];
                    if(preItem){
                        if(item[key]!=preItem[key]) return false;
                    }
                    preItem = item;
                }
                var type = (lastitem[key]?"up":"down");
                var color = (lastitem[key]?"red":"green");
                var msg = `<span class='${color}'>${lastitem.col1}&nbsp;money<br/>&nbsp;&nbsp;&nbsp;&nbsp;${name}&nbsp;${type}</span>`
                
                return this.addException(msg,lastitem[key]);
            }
        },
        addException(msg,isUp,type){
            var tmplist = this.exceptionList.filter((item)=>{return item==msg});
            if(tmplist.length!=0) return false;
            this.exceptionList.push(msg);
            if(!window.electron){
                return true;
            }
            var cur = this.$dayjs().format("HH:mm:ss");
            if(cur>"15:00:00") return true;
            electron.ipcRenderer.send("showBox",msg,type);
            // this.$notify({ 
            //     // title: type, 
            //     message: msg, 
            //     type: isUp?"warning":"info" ,
            //     // duration : 0,
            //     dangerouslyUseHTMLString:true}
            // );
            this.$nextTick(()=>{
                this.$refs.exceptionEl.scrollTop = this.$refs.exceptionEl.scrollHeight;
            });
            return true;
        },

        openLoginPage(){
            
            electron.ipcRenderer.send("showDFCF")

            // window.open("https://jy.xzsec.com/Search/Deal");

            // window.open("https://jy.xzsec.com/Login");
        },
    }
    
}
</script>
